<!--
 * @Author: anshengtyp
 * @Date: 2023-12-13 10:54:22
 * @LastEditTime: 2023-12-14 09:03:27
 * @LastEditors: anshengtyp
 * @Descriptio： 首页部门任务统计
-->
<template>
  <div id="deptTaskCount">
  </div>
</template>

<script>
const animationDuration = 1500
import * as echarts from 'echarts';
require('echarts/theme/macarons')
import resize from '../dashboard/mixins/resize'
import { getIncompleteTasks } from "@/api/indexCount.js";

export default {
  mixins: [resize],
  props: ['dept'],
  data() {
    return {
      chart: null,
      deptList: [],
      deptData: []
    };
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  mounted() {
  },
  watch: {
    dept: {
      async handler(val) {
        const res = await getIncompleteTasks();
        for (const key in res.data) {
          if (Object.hasOwn(res.data, key)) {
            const value = res.data[key];
            let target = val.find(item => item.deptId === key);
            if (target) {
              this.deptList.push(target.deptName);
              this.deptData.push(+value);
            }
          }
        }
        this.initChart();
      },
    }
  },

  methods: {
    initChart() {
      this.chart = echarts.init(this.$el)
      this.chart.setOption({
        title: {
          text: '部门在办任务统计'
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: { // 坐标轴指示器，坐标轴触发有效
            type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
          }
        },
        grid: {
          top: '20%',
          left: '7%',
          right: '2%',
          bottom: '0%',
          containLabel: true
        },
        color: '#6e94f3',
        xAxis: [{
          type: 'category',
          data: this.deptList,
          axisTick: {
            show: false,
            alignWithLabel: false
          }
        }],
        yAxis: [{
          type: 'value',
          name: '在办任务数量',
          // 居中
          nameLocation: 'middle',
          nameGap: 50,
          axisTick: {
            show: false
          }
        }],
        series: [{
          name: '在办任务',
          type: 'bar',
          stack: 'vistors',
          barWidth: '60%',
          data: this.deptData,
          animationDuration
        }]
      })
    }
  },
};
</script>

<style lang="scss" scoped>
#deptTaskCount {
  width: 100%;
  height: 100%;
}
</style>
